var express = require('express');
var router = express.Router();
var projectModel = require('../tool/model/project.js');
router.get('/', (req, res, next)=> {
  res.render('manproject',{
    roots: req.cookies.root,
  })    // res.end()
});



router.get('/project', (req, res, next)=> {
  projectModel.aggregate([
    {
      $lookup:
        {
          from: "clientuser",
          localField: "projectMark",
          foreignField: "userID",
          as: "data"
        }
   },{
    $match:{"status": {"$lt": 3},'projectMark':{'$eq':req.cookies.id}}
   }],function(err,docs){
    if(err){
      console.log(err);
      return;
  }
    console.log(docs)
    res.send({
      data:docs
      })
  })

});
router.post('/projectupdate',function(req, res, next) {
  let data = req.body; 
      projectTitle=data.projectTitle,
      projectMark=data.projectMark,
      projectUrl=data.projectUrl,
      startTime=data.startTime
      endTime =data.endTime,
      projectDesc=data.projectDesc
  console.log(data)
   projectModel.update({
    projectMark: data.projectMark}, 
     {
       $set:{
       'endTime':endTime,
        'startTime':startTime,
        'projectUrl':projectUrl,
        'projectTitle':projectTitle},
        'projectDesc':projectDesc
      },(err,docs)=>{
    if (err) {
      res.send({
          status: '1',
          type:'error',
          message: err.message,
          result: 'err'
      })
  } else {
      res.send({
          status: '0',
          type:'success',
          message: '更改成功！',
          result: 'suc'
      })
    }
   })
});
router.post('/projectdel',function(req, res, next) {
  let data = req.body; 
      status=data.status,
      projectMark=data.projectMark,
  console.log(data)
   projectModel.update({
     _id: data.projectMark}, 
     {'status':status},(err,docs)=>{
    if (err) {
      res.send({
          status: '1',
          type:'error',
          message: error.message,
          result: 'err'
      })
  } else {
      res.send({
          status: '0',
          type:'success',
          message: '更改成功！',
          result: 'suc'
      })
    }
   })
});
router.post('/addimage',function(req, res, next) {
  var form = new formidable.IncomingForm();
  form.on('field',(field,value)=>{
    // console.log(field);
    // console.log(value);
});
form.on('file',(name,file)=>{
    // console.log(name);
    // console.log(file);
});
form.on('end',()=>{
    // console.log(JSON.stringify(form.lookbehind))
    res.send(JSON.stringify(form.openedFiles[0].path))
})
     form.encoding = 'utf-8';
     form.uploadDir =path.join(resolve('./')+'/public/upload') ;
     form.keepExtensions = true;//保留后缀
     form.maxFieldsSize = 2 * 1024 * 1024;
    // //处理图片
    form.parse(req, function (err, fields, files){
      // let extname = path.extname(files.file.name);
      // let oldpath=form.uploadDir+'/'+files.file.path
      // let newpath = form.uploadDir + '/uploads/' + ttt + ran + extname;
    })
});
module.exports = router;